package yxy.game.pm2.active.command.flop;

import yxy.apple.logger.Logger;
import yxy.cherry.data.bean.actfloptarget;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_翻牌;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.subordinateclass.flop.FlopCard;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;

/**
 * 翻牌 - 抽奖  选择终极大奖
 *
 * @author Dell
 */
@MessageType(proto = Command.ActFlopCardSelectDraw, classtype = ActiveMessage.ActFlopCardSelectDrawRequest.class)
public class CMD_Active_翻牌_抽奖_Select extends Command<ActiveMessage.ActFlopCardSelectDrawRequest> {

    private static final Logger logger = Logger.create(CMD_Active_翻牌_抽奖_Select.class);

    @Override
    public void execute(MessageSource messageSource, ActiveMessage.ActFlopCardSelectDrawRequest request) throws Exception {
        String playercode = messageSource.getTag();
        int id = request.getId();
        ActiveMessage.ActFlopCardSelectDrawResponse.Builder response = ActiveMessage.ActFlopCardSelectDrawResponse.newBuilder();

        ActiveModule_翻牌 am = ActiveModule_翻牌.get(playercode, false);
        if (am == null) {
            send(messageSource, response, Result.ActNoOpen);
            return;
        }

        if (!am.running()) {
            send(messageSource, response, Result.ActNoOpen);
            return;
        }

        actfloptarget data = ActDataCenter.getData(id, actfloptarget.class, playercode);
        if (data == null) {
            send(messageSource, response, Result.DataError);
            return;
        }

        if (am.getSelectTimes() != null && am.getSelectTimes().get(id) != null && data.getChoosetimes() != -1 && am.getSelectTimes().get(id) >= data.getChoosetimes()) {//选择次数达到上限
            send(messageSource, response, Result.ActSelectMax);
            return;
        }

        for (FlopCard fc : am.getFcmap().values()) {
            if (fc.isFlop() && fc.isGrand()) {
                send(messageSource, response, Result.ActHadGet);
                return;
            }
        }

        am.setSelectid(data.getId());

        am.set();
        send(messageSource, response, Result.Success);
    }

    private void send(MessageSource messageSource, ActiveMessage.ActFlopCardSelectDrawResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
